package org.dtrd.modules.patient.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.dtrd.base.BaseController;
import org.dtrd.common.api.vo.Result;
import org.dtrd.config.system.SystemState;
import org.dtrd.config.system.SystemState.ResponseState;
import org.dtrd.modules.base.service.BaseCommonService;
import org.dtrd.modules.doctor.entity.po.DtrdEntRdDoctorTeam;
import org.dtrd.modules.doctor.service.IDtrdEntRdDoctorTeamService;
import org.dtrd.modules.patient.entity.bean.miniprogram.PatientMiniProgramIndexInfo;
import org.dtrd.modules.patient.entity.po.DtrdEntRdPatient;
import org.dtrd.modules.patient.entity.request.PatientScanRequest;
import org.dtrd.modules.patient.service.IDtrdEntRdPatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static org.dtrd.common.constant.CommonConstant.OPERATE_TYPE_QUERY;

/**
 * @author qr
 * @date 2022/3/16 13:38
 */
@Api(tags = "患者端小程序")
@RestController
@RequestMapping("/patient/miniprogram/")
@Slf4j
public class PatientMiniProgramController extends BaseController {

    @Autowired
    private IDtrdEntRdPatientService patientService;
    @Autowired
    private IDtrdEntRdDoctorTeamService doctorTeamService;
    @Autowired
    private BaseCommonService baseCommonService;

    @ApiOperation(value = "获取小程序首页信息", response = PatientMiniProgramIndexInfo.class)
    @GetMapping("index")
    public Result<?> getPatientMiniProgramIndexInfo(@RequestParam Integer patientId) {
        if (!patientService.isValidPatientId(patientId)) {
            return error(SystemState.ResponseState.STATE_PARAMS_ERROR);
        }
        log.info("患者id: {} 登陆小程序", patientId);
        PatientMiniProgramIndexInfo indexInfo = patientService.getPatientMiniProgramIndexInfo(patientId);
        String logContent = String.format("获取小程序首页信息, 患者id: %d", patientId);
        baseCommonService.addLog(logContent, OPERATE_TYPE_QUERY);
        return Result.OK(indexInfo);
    }

    @ApiOperation("扫描医生二维码")
    @PostMapping("/scan")
    public Result<?> onScanDoctorQrCode(@RequestBody @Validated PatientScanRequest request) {
        log.info("PatientMiniProgramController.getApi.[request] " + request);
        boolean result = patientService.onScanDoctorQrCode(request.getUserId(), request.getQrcodeUrl());
        String teamName = null;
        if (result) {
            DtrdEntRdPatient patient = patientService.getById(request.getUserId());
            if (patient.getDoctorId() != null) {
                DtrdEntRdDoctorTeam doctorTeam = doctorTeamService.getDoctorTeamByDoctorId(patient.getDoctorId());
                if (doctorTeam != null) {
                    teamName = doctorTeam.getName();
                }
            }
        }
        return result ? Result.OK(teamName) : error(ResponseState.STATE_OPERATION_FAILURE);
    }
}
